package com.doujiang.Controller;


import com.doujiang.domain.dto.PageResult;
import com.doujiang.domain.dto.jhjMovieDTO;
import com.doujiang.service.jhjIMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/movie/movie")
public class jhjMovieController {

    @Autowired
    private jhjIMovieService movieService;

    private static final int DEFAULT_MIN_VOTES = 5;
    private static final int DEFAULT_MIN_VOTES_FOR_Nolan= 3;

    @GetMapping("/top-suspense")
    public List<jhjMovieDTO> getTopSuspenseMovies() {
        return movieService.getTopSuspenseMovies(DEFAULT_MIN_VOTES);
    }

    @GetMapping("/top-high-score")
    public List<jhjMovieDTO> getHighScoreMovies() {
        return movieService.getHighScoreMovies(DEFAULT_MIN_VOTES);
    }

    @GetMapping("/top-latest")
    public List<jhjMovieDTO> getLatestMovies() {
        return movieService.getLatestMovies(DEFAULT_MIN_VOTES);
    }

    @GetMapping("/top-action-thriller")
    public List<jhjMovieDTO> getActionThrillerMovies() {
        return movieService.getActionThrillerMovies(DEFAULT_MIN_VOTES);
    }

    @GetMapping("/top-fantasy-scifi")
    public List<jhjMovieDTO> getFantasySciFiMovies() {
        return movieService.getFantasySciFiMovies(DEFAULT_MIN_VOTES);
    }

    @GetMapping("/top-nolan")
    public List<jhjMovieDTO> getNolanMovies() {
        return movieService.getNolanMovies(DEFAULT_MIN_VOTES_FOR_Nolan);
    }

    @GetMapping("/top-chinese")
    public List<jhjMovieDTO> getChineseMovies() {
        return movieService.getChineseMovies(DEFAULT_MIN_VOTES);
    }
    @GetMapping("/rank/{name}/more")
    public PageResult<jhjMovieDTO> getMoreMovies(
            @PathVariable("name") String name,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "5") int size
    ) {
        System.out.println("接收到请求参数 - name: " + name + ", page: " + page + ", size: " + size);
        double globalAvg = movieService.getGlobalAvgScore();
        int minVotes;
        if ("nolan".equals(name)) {
            minVotes = DEFAULT_MIN_VOTES_FOR_Nolan;
        } else if ("latest".equals(name)) {
            minVotes = 1;
        } else {
            minVotes = DEFAULT_MIN_VOTES;
        }
        // 返回带总数的分页结果
        PageResult<jhjMovieDTO> result = movieService.getMoviesByType(name, minVotes, globalAvg, page, size);
        System.out.println("返回结果 - 总数: " + result.getTotal() + ", 记录数: " + result.getRecords().size());
        return result;
    }

}
